<%@ page contentType="text/html; charset=UTF-8" autoFlush="false"
	import="java.util.*,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*,com.engc.common.utils.Constant;"%>
<%
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");
	response.setContentType("text/html; charset=UTF-8");
%>
<%
	String chars = "0123456789abcdefghijklmnopqrstuvwxyz";
	char[] rands = new char[4];
	for (int i = 0; i < 4; i++) {
		int rand = (int) (Math.random() * 36);
		rands[i] = chars.charAt(rand);
	}
	session.setAttribute(Constant.VALIDATE_CODE_SESS, new String(rands));
%>
<%
	out.clear();
	response.setContentType("image/jpeg");
	response.addHeader("pragma", "NO-cache");
	response.addHeader("Cache-Control", "no-cache");
	response.addDateHeader("Expries", 0);
	int width = 60, height = 20;

	BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	Graphics g = image.getGraphics();

	g.setColor(new Color(0xDCDCDC));
	g.fillRect(0, 0, width, height);
	// 随机产生120个干扰点
	for (int i = 0; i < 120; i++) {

		int x = (int) (Math.random() * width);
		int y = (int) (Math.random() * height);
		int red = (int) (Math.random() * 255);
		int green = (int) (Math.random() * 255);
		int blue = (int) (Math.random() * 255);
		g.setColor(new Color(red, green, blue));
		g.drawOval(x, y, 1, 0);
	}

	g.setColor(Color.BLACK);
	g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 16));

	// 在不同的高度上输出验证码的每个字符
	g.drawString("" + rands[0], 1, 17);
	g.drawString("" + rands[1], 16, 15);
	g.drawString("" + rands[2], 31, 18);
	g.drawString("" + rands[3], 46, 16);
	g.dispose();

	ServletOutputStream outStream = response.getOutputStream();
	JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream);
	encoder.encode(image);
	outStream.close();
	response.flushBuffer();
	out.clear();
	out = pageContext.pushBody();
%>
